Skip to content

Conversation

@xterao
Copy link
Collaborator

@xterao xterao commented Jul 18, 2025

When conditional or loop directives (e.g., if, for) are nested, the formatter increases the indentation level for each nested directive.

If a directive is placed within a different group block, the indentation is not based on the outer directive's indentation, but rather:

It aligns with the nearest enclosing group block's indentation.

 /*%if includesDepartment */
 LEFT JOIN department d
        ON e.department_id = d.id
   /*%if includeDepartmentDetails */
   LEFT JOIN department_detail dd
          ON d.id = dd.department_id
   /*%end */
 /*%end */

Additional Formatting Rules Implemented

  • Insert 1 space after embedded variable directives
    Ensure there is a space between an embedded variable directive (e.g., /*# variable */) and the following element.

  • Standard comment blocks are not managed in parent-child block hierarchy
    When comments are line-broken, align them to the indentation level of the immediate next line rather than treating them as part of any structural group.

  • Conditional / Loop directives line-break rule
    If a conditional or loop directive is not the first element in a sub-group, insert a line break before the directive to enhance readability.

@xterao xterao self-assigned this Jul 18, 2025
@xterao xterao requested a review from Copilot July 18, 2025 08:19

This comment was marked as outdated.

@xterao xterao force-pushed the feature/sql-format-nest-directive branch from de21613 to 514d571 Compare July 18, 2025 10:34
@xterao xterao requested a review from Copilot July 18, 2025 10:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements enhanced formatting rules for SQL conditional and loop directives, adding proper indentation and spacing handling for nested directive blocks. The changes improve code readability by managing hierarchical block structures and ensuring consistent formatting across complex SQL templates.

  • Enhanced indentation adjustment for nested conditional/loop directives with proper parent-child block hierarchy management
  • Added space insertion after embedded variable directives and improved line-break rules for conditional/loop directives
  • Refactored block processing architecture to properly handle directive comment blocks within the formatting pipeline

Reviewed Changes

Copilot reviewed 43 out of 44 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/test/testData/sql/formatter/*.sql Test data files demonstrating expected formatting output for nested directives and conditional blocks
src/test/kotlin/.../SqlFormatterTest.kt Added test methods for nested directives and conditional case formatting
src/main/kotlin/.../util/SqlKeywordUtil.kt Added "select" keyword with "if exists" clause support
src/main/kotlin/.../util/SqlBlockUtil.kt Refactored comma block creation logic and moved comment block imports
src/main/kotlin/.../util/CommaRawUtil.kt New utility class to handle comma block creation with conditional directive support
src/main/kotlin/.../processor/SqlSetParentGroupProcessor.kt Major refactoring of parent group processing with enhanced conditional directive handling
src/main/kotlin/.../processor/SqlPostProcessor.kt Improved code organization with extracted helper methods for document processing
src/main/kotlin/.../builder/SqlCustomSpacingBuilder.kt Added spacing logic for directive comments and simplified right pattern handling
src/main/kotlin/.../builder/SqlBlockBuilder.kt Enhanced comment and conditional directive block management with improved indentation logic
src/main/kotlin/.../block/comment/*.kt Moved and refactored comment block classes with new conditional directive support
src/main/kotlin/.../block/*.kt Updated various block classes to support conditional directive indentation

@xterao xterao force-pushed the feature/sql-format-nest-directive branch from 514d571 to 3821fe7 Compare July 18, 2025 11:02
@xterao xterao merged commit 28c32fd into feature/sql-format-official-version Jul 22, 2025
7 of 8 checks passed
@xterao xterao deleted the feature/sql-format-nest-directive branch July 22, 2025 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment